from flask import Blueprint,request,redirect,url_for
from .. import lm
from flask_login import UserMixin,current_user,login_user,login_required
v1 = Blueprint("v1",__name__)

class User(UserMixin):
    pass

@lm.user_loader
def user_loader(id):
    u = User()
    u.id = id
    u.text = "user_loader"
    return u

@v1.route("/index")
def index():
    return "v1 index"


@v1.route("/login",methods = ['GET','POST'])
def login():
    if request.method == "GET":
        return  '''
               <form action='login' method='POST'>
                <input type='text' name='email' id='email' placeholder='email'></input>
                <input type='password' name='pw' id='pw' placeholder='password'></input>
                <input type='submit' name='submit'></input>
               </form>
               '''
    id = request.form.get("email")
    tmp = User()
    tmp.id = id
    login_user(tmp)
    return redirect(url_for("v1.person"))


@v1.route("/person")
@login_required
def person():
    return "you are loged in " + current_user.id + current_user.text


